package com.example.msg;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.example.msg.utils.HttpUtils;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class NotifyService extends NotificationListenerService {
    public static final String TAG = "HELLO1:";


    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Notification notis= sbn.getNotification();
        String packag=sbn.getPackageName();
        String cannelId=notis.getChannelId();
        Log.i(TAG,packag);
        if(notis!=null){
            if(notis.extras!=null){
                //Object title=notis.extras.get("android.title");
                //Object subText=notis.extras.get("android.subText");
                //Object infoText=notis.extras.get("android.infoText");
                String text="";

                Object temp=notis.extras.get(Notification.EXTRA_TEXT);
                if(temp!=null){
                    text=(String)temp;

                }
                //Log.i(TAG,text);
                //当包名是华为语音助手时候
                if("com.huawei.vassistant".equals(packag)){
                    Log.i(TAG,"接收到华为助手的消息:"+text);
                    if(text!=null||!"".equals(text)){
                        Toast.makeText( this,text,Toast.LENGTH_LONG).show();
                        sendBordCast(text);
                    }
                }
            }
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG,"onBind");
        return super.onBind(intent);

    }

    @Override
    public void onListenerDisconnected() {
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
            // 通知侦听器断开连接 - 请求重新绑定
            requestRebind(new ComponentName(this,NotificationListenerService.class));
        }
    }
    //在局域网发送广播
    private  void sendBordCast(String msg){
        new Thread(){
            @Override
            public void run() {
                try{

                    String dataJson="{\"data433\":\""+msg+"\"}";
                    Log.i(TAG, "run: "+dataJson);
                    String url="http://api.heclouds.com/cmds?device_id=933278607";
                    String json=HttpUtils.post(url,null,dataJson);
                    Log.i(TAG, "run: "+json);

//                    DatagramSocket socket = new DatagramSocket();
//                    byte[] data = msg.getBytes();
//                    int len=data.length;
//                    byte[] result=new byte[data.length+2];
//                    result[0]=(byte)(len&0xFF);
//                    result[1]=(byte)((len>>8)&0xFF);
//                    for(int i=0;i<len;i++){
//                        result[i+2]=data[i];
//                    }
//                    DatagramPacket packet = new DatagramPacket(result,result.length, InetAddress.getByName("255.255.255.255"),6666);
//                    // 3.发送数据
//                    socket.send(packet);
//                    socket.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();


    }
}
